package com.example.user.service.dubbo.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.commons.core.bean.UserInfoRightBean;
import com.example.commons.core.bean.UserInfoRoleBean;
import com.example.commons.core.entity.UserInfo;
import com.example.commons.core.entity.UserRoleRelation;
import com.example.system.api.service.UserInfoDubboService;
import com.example.user.service.RightService;
import com.example.user.service.RoleService;
import com.example.user.service.UserInfoService;
import com.example.user.service.UserRoleRelationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
@Component
public class UserInfoDubboServiceImpl implements UserInfoDubboService {

    private final UserInfoService userInfoService;
    private final RoleService roleService;
    private final RightService rightService;
    private final UserRoleRelationService userRoleRelationService;

    public UserInfoDubboServiceImpl(UserInfoService userInfoService, RoleService roleService, RightService rightService, UserRoleRelationService userRoleRelationService) {
        this.userInfoService = userInfoService;
        this.roleService = roleService;
        this.rightService = rightService;
        this.userRoleRelationService = userRoleRelationService;
    }

    @Override
    public UserInfo getById(Long userId) {
        return userInfoService.getById(userId);
    }

    @Override
    public UserInfoRightBean rightAndUserFindByUserId(Long userId) {
        UserInfo userInfo = userInfoService.getById(userId);
        UserInfoRightBean userInfoRightBean = new UserInfoRightBean();
        BeanUtil.copyProperties(userInfo, userInfoRightBean);
        userInfoRightBean.setRightList(rightService.rightFindByUserId(userId));
        return userInfoRightBean;
    }

    @Override
    public UserInfoRoleBean roleAndUserFindByUserId(Long userId) {
        UserInfoRoleBean userInfoRightBean = (UserInfoRoleBean) userInfoService.getById(userId);
        userInfoRightBean.setRoleList(roleService.roleFindByUserId(userId));
        return userInfoRightBean;
    }

    @Override
    public List<Long> userRoleRelationFindByUserId(Long userId) {
        List<UserRoleRelation> roleRelationList = userRoleRelationService.list(new LambdaQueryWrapper<UserRoleRelation>().eq(UserRoleRelation::getUserId, userId));
        return roleRelationList.stream().map(UserRoleRelation::getRoleId).collect(Collectors.toList());
    }
}
